﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections ;

using YT.Commons;
using YT.DB;
using YT.Modules; 

namespace YT.XH.MeniereDisease
{
    public partial class frmMDJacobsonTitle : Form
    {
        private ArrayList marrlist = new ArrayList();
        private string mstrSickRecID = string.Empty;
        private DBAccess mdbcon = new DBAccess(false, false);
        private string mstrHdrTableName = "JocobsonHdr";
        private string mstrResultTableName = "JocobsonResult";
        private FormStatus mfrmStatus;
        private string mstrSeqNo = string.Empty;
        private bool mblnExit = false;
        private JobocoType mJobocoType = JobocoType.TreatEffect;
        public frmMDJacobsonTitle()
        {
            InitializeComponent();
        }
        public frmMDJacobsonTitle(string strSickRecID, FormStatus frmStatus)
        {
            InitializeComponent();
            mstrSickRecID = strSickRecID;
        }

        public frmMDJacobsonTitle(string strSickRecID, FormStatus frmStatus,string strSeqNo )
        {
            InitializeComponent();
            mstrSickRecID = strSickRecID;
            mfrmStatus = frmStatus;
            mstrSeqNo = strSeqNo;
        }

        public frmMDJacobsonTitle(string strSickRecID, FormStatus frmStatus, string strSeqNo, JobocoType jobocoType)
        {
            InitializeComponent();
            mstrSickRecID = strSickRecID;
            mfrmStatus = frmStatus;
            mstrSeqNo = strSeqNo;
            mJobocoType = jobocoType;
        }

        public bool GetTotalJacobson(ref double dblTotalScore, ref double dblFscore, ref double dblEscore, ref double dblPscore, ref double dblSubScore)
        {
            this.ShowDialog();
            dblTotalScore =CvtFunc.ToDouble ( txtTotalScore.Text);
            dblFscore = CvtFunc.ToDouble(txtFscore.Text);
            dblEscore = CvtFunc.ToDouble(txtEscore.Text);
            dblPscore = CvtFunc.ToDouble(txtPscore.Text);
            if (mblnExit)
                return false;
            else
                return true;
           
        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            if (UpdateHdr()) this.Close();
        }

        private bool UpdateHdr()
        {
            CalScoreAndSub();
            string strSeqNo = string.Empty;
            DBAccess dbcon = new DBAccess(false, false);
            string strSQL = string.Empty;
            //if (mfrmStatus == FormStatus.AddNew)
            //{
            //    strSeqNo = DataFunctions.GetFieldValue(mstrHdrTableName, "max(SeqNo) as SeqNo", "1=1 and Type='" + TitleType.MeniereDisease + "' and SickRecID='"+ mstrSickRecID  +"'");
            //    strSeqNo = CvtFunc.ToString(CvtFunc.ToInt(strSQL) + 1);
            //    strSQL = " Insert into " + mstrHdrTableName + " (  SickRecID, SeqNo, ExamDate,Type ) " +
            //             " values (  '" + mstrSickRecID + "','" + strSeqNo + "','" + BaseFunctions.FormatDate(DateTime.Now.Date.ToString(), DateFormatOption.DateFmtYMD) + "' " +
            //             " ,'" + TitleType.MeniereDisease + "' ) ";
            //    dbcon.ExecuteSql(strSQL);
            //}
            //else if (mfrmStatus == FormStatus.Update)
            //{
            //    strSeqNo = mstrSeqNo;
            //}
            UpdateData(mstrSeqNo);
            mfrmStatus = FormStatus.Update;
            return true;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否保存？", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                UpdateHdr();
                this.Close();
            }
            else
                this.Close();
        }

        private void FindPanel(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                if (con.GetType().Name == "Panel" && con.Tag != "" && con.Tag != null)
                {
                    marrlist.Add(con);
                }
                if (con.Controls.Count > 0)
                {
                    FindPanel(con);
                }
                if (con.GetType().Name == "RadioButton") ((RadioButton)con).CheckedChanged += new System.EventHandler(this.Calulate);
            }
        }

        private void FillData()
        {            
            DBAccess dbcon = new DBAccess(false, false);
            foreach (Control cons in this.Controls)
            {
                FindPanel(cons);
            }
            foreach (Control conDtl in marrlist)
            {
                if (conDtl.GetType().Name == "Panel")
                {
                    string[] strInfoSeqNo = new string[2];
                    strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                    string strHdrSeqNo = strInfoSeqNo[0];
                    string strDtlSeqNo = strInfoSeqNo[1];
                    string strSickRecID = mstrSickRecID;
                    string strColTitle = string.Empty;
                    string strResult = string.Empty;
                    string strTitleType = TitleType.MeniereDisease.ToString();
                    foreach (Control conText in conDtl.Controls)
                    {
                        if (conText.Tag != "" && conText.Tag != null)
                        {
                            strColTitle = conText.Tag.ToString();
                            string strSQL = "select top 1 Result from " + mstrResultTableName + " where rowno='" + strColTitle + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and Type='" + TitleType.MeniereDisease + "' and ExamSeqNo='" + mstrSeqNo + "' and SickRecID='" + mstrSickRecID + "' and FromModule = '" + mJobocoType + "' ";
                            DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                            if (drCheck == null) break;
                            strResult = CvtFunc.ToString(drCheck["Result"]);
                            if (conText.GetType().Name == "TextBox")
                            {
                                conText.Text = strResult;
                            }
                            else if (conText.GetType().Name == "RadioButton")
                            {
                                if (strResult == "false")
                                    ((RadioButton)conText).Checked = false;
                                else if (strResult == "true")
                                    ((RadioButton)conText).Checked = true;
                            }
                            else if (conText.GetType().Name == "CheckBox")
                            {
                                if (strResult == "false")
                                    ((CheckBox)conText).Checked = false;
                                else if (strResult == "true")
                                    ((CheckBox)conText).Checked = true;
                            }
                            else if (conText.GetType().Name == "Label")
                            {
                                break;
                            }
                        }
                    }

                }
            }
            dbcon.CloseCon();
        }

        private void UpdateData(string strExamSeqNo)
        {            
            DBAccess dbcon = new DBAccess(false, false);
            foreach (Control conDtl in marrlist)
            {
                if (conDtl.GetType().Name == "Panel")
                {
                    string[] strInfoSeqNo = new string[2];
                    strInfoSeqNo = conDtl.Tag.ToString().Split('.');
                    string strHdrSeqNo = strInfoSeqNo[0];
                    string strDtlSeqNo = strInfoSeqNo[1];
                    string strSickRecID = mstrSickRecID;
                    string strColTitle = string.Empty;
                    string strResult = string.Empty;
                    DataRow drRow = null;
                    string strFields = "SickRecID,HdrSeqno,DtlSeqNo,RowNo,ColTitle,Result,Type,ExamSeqNo,FromModule";
                    foreach (Control conText in conDtl.Controls)
                    {
                        if (conText.Tag != "" && conText.Tag != null)
                        {
                            if (conText.GetType().Name == "TextBox")
                            {
                                strResult = conText.Text;
                            }
                            else if (conText.GetType().Name == "RadioButton")
                            {
                                strResult = (((RadioButton)conText).Checked ? "true" : "false");
                            }
                            else if (conText.GetType().Name == "CheckBox")
                            {
                                strResult = (((CheckBox)conText).Checked ? "true" : "false");
                            }
                            else if (conText.GetType().Name == "Label")
                            {
                                strResult = "";
                                break;
                            }
                            strColTitle = conText.Tag.ToString();

                            string strSQL = "select top 1 * from " + mstrResultTableName + " where rowno='" + strColTitle + "' and  HdrSeqNo='" + strHdrSeqNo + "' and DtlSeqNo='" + strDtlSeqNo + "' and ExamSeqNo='" + mstrSeqNo + "' and Type='" + TitleType.MeniereDisease + "' and SickRecID='" + mstrSickRecID + "' and FromModule = '" + JobocoType.TreatEffect + "'";
                            bool blnIsNew = false;
                            DataRow drCheck = dbcon.GetDataFirstRow(strSQL);
                            if (drCheck == null)
                            {
                                blnIsNew = true;
                                drRow = dbcon.GetDataTable("select top 0 * from " + mstrResultTableName + " ").NewRow();
                            }
                            else
                            {
                                blnIsNew = false;
                                drRow = dbcon.GetDataFirstRow("select top 1 * from " + mstrResultTableName + " where recid='" + CvtFunc.ToString(drCheck["RecID"]) + "' ");
                            }

                            drRow["SickRecID"] = strSickRecID;
                            drRow["HdrSeqno"] = strHdrSeqNo;
                            drRow["DtlSeqNo"] = strDtlSeqNo;
                            drRow["RowNo"] = strColTitle;
                            drRow["Result"] = strResult;
                            drRow["Type"] = TitleType.MeniereDisease;
                            drRow["ExamSeqNo"] = strExamSeqNo;
                            drRow["FromModule"] = mJobocoType.ToString (); 
                            if (blnIsNew)
                                DataFunctions.UpdateTable(dbcon, strFields, mstrResultTableName  , drRow, FormStatus.AddNew);
                            else
                                DataFunctions.UpdateTable(dbcon, strFields, mstrResultTableName, drRow, FormStatus.Update);
                        }
                    }
                }
            }
            dbcon.CloseCon();
        }

        private void frmMDJacobsonTitle_Load(object sender, EventArgs e)
        {
            FillData();
            CalScoreAndSub();
            this.Text = "眩晕障碍评估表";
        }

        private void CalScoreAndSub()
        {
            int intResult = 0;
            int intFscore = 0;
            int intEscore = 0;
            int intPscore = 0;
            int intTotalScore = 0;
            int intTotalSubScore = 0;
            foreach (Control conDtl in marrlist)
            {
                if (conDtl.GetType().Name == "Panel")
                {
                    if (conDtl.Name.Substring(0, 4) == "pnl_")
                    {
                        string strType = conDtl.Name.Split('_')[1].Substring(0, 1);
                        foreach (Control conItem in conDtl.Controls)
                        {
                            if (conItem.GetType().Name == "RadioButton")
                            {
                                if (((RadioButton)conItem).Checked)
                                {
                                    intResult = BaseFunctions.GetScoreFunction(conItem.Tag.ToString());
                                }
                                if (strType == "E") intEscore = intEscore + intResult;
                                if (strType == "F") intFscore = intFscore + intResult;
                                if (strType == "P") intPscore = intPscore + intResult;
                                intResult = 0;
                            }
                        }
                    }
                    else if (conDtl.Name.Length >= 8 && conDtl.Name.Substring(0, 7) == "pnlsub_")
                    {
                        int intSubResult = 0;
                        foreach (Control conItem in conDtl.Controls)
                        {
                            if (conItem.GetType().Name == "RadioButton")
                            {
                                if (((RadioButton)conItem).Checked)
                                {
                                    intSubResult = BaseFunctions.GetScoreFunction(conItem.Tag.ToString());
                                }
                            }
                            intTotalSubScore = intTotalSubScore + intSubResult;
                            intSubResult = 0;
                        }


                    }
                }
            }

            intTotalScore = intFscore + intEscore + intPscore;
            txtEscore.Text = CvtFunc.ToString(intEscore);
            txtPscore.Text = CvtFunc.ToString(intPscore);
            txtFscore.Text = CvtFunc.ToString(intFscore);

            txtTotalScore.Text = CvtFunc.ToString(intTotalScore);
            

        }
        private void Calulate(object sender, EventArgs e)
        {
            CalScoreAndSub();
        }
        private void frmMDJacobsonTitle_FormClosed(object sender, FormClosedEventArgs e)
        {
            SysVar.marrFormList.Remove(this.Name);
        }
    }
}
